home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MPW_TOOL / TOOLS / TOOLS_WI / BYACC__ / BYACCPAR < prev    next >
Text File  |  1989-11-19  |  6KB  |  348 lines

  1.  
  2. /************************************************************************
  3. *                                                                        *
  4. *                             An LR(1) Parser                            *
  5. *                                                                        *
  6. *                                   by                                    *
  7. *                                                                        *
  8. *                           Robert Paul Corbett                            *
  9. *                                                                        *
  10. *                            1986 November 15                            *
  11. *                                                                        *
  12. ************************************************************************/
  13.  
  14. @@
  15.  
  16. #ifndef    YYSTACKSIZE
  17. #define    YYSTACKSIZE    512
  18. #endif
  19.  
  20. extern int yylex();
  21. extern yyerror();
  22. extern int write();
  23.  
  24. int yydebug;
  25. int yypatched;
  26. int yynchars;
  27. int yychar, yychar2;
  28. YYSTYPE    yyval, yylval, yylval2;
  29. short yycs[YYSTACKSIZE];
  30. short yyss[YYSTACKSIZE];
  31. YYSTYPE yyvs[YYSTACKSIZE];
  32. short *yyssp;
  33. YYSTYPE *yyvsp;
  34.  
  35. yyverbose(s, n)
  36. register char *s;
  37. register int n;
  38. {
  39.     char message[32];
  40.     char number[20];
  41.     register char *t;
  42.  
  43.     t = message;
  44.     while (*t++ = *s++);
  45.  
  46.     if (n <= 0)
  47.     *t++ = '0';
  48.     else
  49.     {
  50.       s = number + 19;
  51.       *s = 0;
  52.       while (n > 0)
  53.     {
  54.         *--s = '0' + (n % 10);
  55.         n = n / 10;
  56.     }
  57.  
  58.         while (*t++ = *s++);
  59.     }
  60.  
  61.     *t++ = '\n';
  62.     (void) write(2, message, t - message);
  63. }
  64.  
  65. int
  66. yyparse()
  67. {
  68.     register int yyx;
  69.     register int yyc;
  70.     register int yyn;
  71.     register int yystate;
  72.     register short *yysp;
  73.     register YYSTYPE *yyvp;
  74.     register int yystatus;
  75.     register short *yytmp;
  76. #ifdef YYDEFCHK
  77.     register int yyok;
  78.  
  79.     yyok = 0;
  80. #endif
  81.     yystatus = 0;
  82.     yynchars = 0;
  83.     yystate = 0;
  84.     yysp = yyss;
  85.     yyvp = yyvs;
  86.     *yysp = 0;
  87.  
  88.     for (;;)
  89.     {
  90.     yyx = yyrowbase[yystate];
  91.     if (yyx >= 0)
  92.         goto yyreduce;
  93.  
  94.     if (yynchars > 0)
  95.         yyc = yychar;
  96.     else
  97.     {
  98.         yyc = yylex();
  99.         if (yyc < 0)
  100.         yyc = 0;
  101.         yychar = yyc;
  102.         yynchars = 1;
  103.     }
  104.  
  105.     yyx += yyc;
  106.     if (yyx <= 0)
  107.     {
  108.         yyx += YYTABLESIZE;
  109.         if (yyx >= 0 && yycheck[yyx] == yyc)
  110.         {
  111.         yyx = yytable[yyx];
  112.         if (yyx > 0)
  113.             goto yyshift;
  114.         yyx = -yyx;
  115.         goto yyreduce;
  116.         }
  117.     }
  118.  
  119.     yyx = yydefred[yystate];
  120. #ifdef YYDEFCHK
  121.     if (yyok)
  122.         goto yyreduce;
  123.     if (yyx >= 0)
  124.     {
  125.         yytmp = yysp;
  126.         yyn = -1;
  127.         for (;;)
  128.         {
  129.         yyn -= yyrhslm1[yyx];
  130.         if (yyn > 0)
  131.         {
  132.             if (yyn >= YYSTACKSIZE)
  133.             return (2);
  134.             yystate = yycs[yyn - 1];
  135.         }
  136.         else
  137.         {
  138.             yysp += yyn;
  139.             yystate = *yysp;
  140.             yyn = 0;
  141.         }
  142.  
  143.         yyc = yylhs[yyx];
  144.         yyx = yycolumnbase[yyc] + yystate;
  145.         if (yyx >= 0 && yyx <= YYTABLESIZE && yycheck[yyx] == yystate)
  146.             yystate = yytable[yyx];
  147.         else
  148.             yystate = yydefgoto[yyc];
  149.         yycs[yyn] = yystate;
  150.  
  151.         yyx = yyrowbase[yystate];
  152.         if (yyx >= 0)
  153.             continue;
  154.         yyc = yychar;
  155.         yyx += yyc;
  156.         if (yyx <= 0)
  157.         {
  158.             yyx += YYTABLESIZE;
  159.             if (yyx >= 0 && yycheck[yyx] == yyc)
  160.             {
  161.             yyx = yytable[yyx];
  162.             if (yyx > 0)
  163.             {
  164. yyacceptable:
  165.                 yyok = 1;
  166.                 yysp = yytmp;
  167.                 yystate = *yysp;
  168.                 yyx = yydefred[yystate];
  169.                 goto yyreduce;
  170.             }
  171.             yyx = -yyx;
  172.             continue;
  173.             }
  174.         }
  175.  
  176.         if (yystate == YYQFINAL && yyc == 0)
  177.             goto yyacceptable;
  178.         yysp = yytmp;
  179.         yystate = *yysp;
  180.         break;
  181.         }
  182.     }
  183. #else
  184.     if (yyx >= 0)
  185.         goto yyreduce;
  186. #endif
  187.  
  188.         if (yystate == YYQFINAL && yyc == 0)
  189.         return (0);
  190.  
  191.         if (yystatus > 0 || yyc == YYERRCODE)
  192.         {
  193.         if (yyc == 0)
  194.             return (1);
  195.         yystatus = 3;
  196.         if (yydebug && yyc != YYERRCODE)
  197.             yyverbose("discard token ", yyc);
  198.         yynchars--;
  199.         if (yynchars > 0)
  200.         {
  201.             yychar = yychar2;
  202.             yylval = yylval2;
  203.         }
  204.         continue;
  205.         }
  206.  
  207.         yyssp = yysp;
  208.         yyvsp = yyvp;
  209.         yypatched = 0;
  210.         yyerror("syntax error");
  211.         yysp = yyssp;
  212.         yyvp = yyvsp;
  213.         yystate = *yysp;
  214.         if (yypatched)
  215.         continue;
  216.  
  217. yyrecover:
  218.         if (yydebug)
  219.         (void) write(2, "syntax error detected\n", 22);
  220.         yytmp = yysp;
  221.         yyn = -1;
  222.         for (;;)
  223.         {
  224.         yyx = yyrowbase[yystate];
  225.         if (yyx >= 0)
  226.             goto yyereduce;
  227.         yyx += YYERRCODE;
  228.         if (yyx <= 0)
  229.         {
  230.             yyx += YYTABLESIZE;
  231.             if (yyx >= 0 && yycheck[yyx] == YYERRCODE)
  232.             {
  233.             yyx = yytable[yyx];
  234.             if (yyx > 0)
  235.                 break;
  236.             yyx = -yyx;
  237.             goto yyereduce;
  238.             }
  239.         }
  240.  
  241.         yyx = yydefred[yystate];
  242.         if (yyx >= 0)
  243.             goto yyereduce;
  244.  
  245.         yysp = yytmp;
  246.         if (yydebug)
  247.             yyverbose("pop state ", *yysp);
  248.         yysp--;
  249.         if (yysp < yyss)
  250.             return (1);
  251.         yytmp = yysp;
  252.         yystate = *yysp;
  253.         continue;
  254.  
  255. yyereduce:
  256.         yyn -= yyrhslm1[yyx];
  257.         if (yyn > 0)
  258.         {
  259.             if (yyn > YYSTACKSIZE)
  260.             return (2);
  261.              yystate = yycs[yyn - 1];
  262.         }
  263.         else
  264.         {
  265.             yysp += yyn;
  266.             yystate = *yysp;
  267.             yyn = 0;
  268.         }
  269.  
  270.         yyc = yylhs[yyx];
  271.         yyx = yycolumnbase[yyc] + yystate;
  272.         if (yyx >= 0 && yyx <= YYTABLESIZE && yycheck[yyx] == yystate)
  273.             yystate = yytable[yyx];
  274.         else
  275.             yystate = yydefgoto[yyc];
  276.         }
  277.  
  278.         yysp = yytmp;
  279.         yyvp = yyvs + (yysp - yyss);
  280.         yystate = *yysp;
  281.         yynchars = 2;
  282.         yychar2 = yychar;
  283.         yylval2 = yylval;
  284.         yychar = YYERRCODE;
  285. #ifdef YYDEFCHK
  286.         yyok = 1;
  287. #endif
  288.         yystatus = 3;
  289.         continue;
  290.  
  291. yysignal:
  292.         yysp += yyn;
  293.         yyvp += yyn;
  294.         goto yyrecover;
  295.  
  296. yyshift:
  297.         if (yysp >= yyss + YYSTACKSIZE)
  298.         return (2);
  299.         *++yysp = yystate = yyx;
  300.         *++yyvp = yylval;
  301. #ifdef YYDEFCHK
  302.     yyok = 0;
  303. #endif
  304.         if (yystatus > 0)
  305.         yystatus--;
  306.         if (yydebug)
  307.         yyverbose("shift state ", yystate);
  308.         yynchars--;
  309.         if (yynchars > 0)
  310.     {
  311.         yychar = yychar2;
  312.         yylval = yylval2;
  313.     }
  314.         continue;
  315.  
  316. yyreduce:
  317.         if (yydebug)
  318.         yyverbose("reduce rule ", yyx);
  319.         yyn = yyrhslm1[yyx];
  320.         yysp -= yyn;
  321.         yyvp -= yyn;
  322.         if (yysp >= yyss + YYSTACKSIZE)
  323.         return (2);
  324.  
  325.         switch (yyx)
  326.     {
  327. #define    YYACCEPT    return (0)
  328. #define    YYERROR        goto yysignal
  329. #define yyerrok        yystatus = 0
  330. #define    yyclearin    yynchars = 0
  331. @@
  332.     default:
  333.         goto yydonothing;
  334.     }
  335.         *yyvp = yyval;
  336.  
  337. yydonothing:
  338.         yystate = yysp[-1];
  339.         yyc = yylhs[yyx];
  340.         yyx = yycolumnbase[yyc] + yystate;
  341.         if (yyx >= 0 && yyx <= YYTABLESIZE && yycheck[yyx] == yystate)
  342.         yystate = yytable[yyx];
  343.         else
  344.         yystate = yydefgoto[yyc];
  345.         *yysp = yystate;
  346.     }
  347. }
  348.